package components.alert
{
	import capricornus.components.ComponentsManager;
	import capricornus.components.window.WindowAlignType;
	import capricornus.components.window.event.WindowEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;

	internal class AlertController
	{
		private var _view:AlertView;
		
		private var _okFun:Function;
		private var _cancelFun:Function;
		
		public function AlertController()
		{
			init();
		}
		
		private function init():void
		{
			_view = new AlertView( ComponentsManager.instance.popupLayer );
			_view.setCenterLocation();
			_view.setAlignType( WindowAlignType.SCALE, WindowAlignType.SCALE );
			_view.show();
			
			_view.btnOK.addEventListener( MouseEvent.CLICK, onClick, false, 0, true );
			_view.btnCancel.addEventListener( MouseEvent.CLICK, onClick, false, 0, true );
			_view.addEventListener( WindowEvent.WINDOW_CLOSED, onClose, false, 0, true );
		}
		
		public function destroy():void
		{
			_view.btnOK.removeEventListener( MouseEvent.CLICK, onClick );
			_view.btnCancel.removeEventListener( MouseEvent.CLICK, onClick );
			_view.removeEventListener( WindowEvent.WINDOW_CLOSED, onClose );
			_view.destroy();
			_okFun = null;
			_cancelFun = null;
			
			Alert.remove( this );
		}
		
		public function setInfo( content:String, okFun:Function, cancelFun:Function ):void
		{
			_view.txtContent.htmlText = content;
			_okFun = okFun;
			_cancelFun = cancelFun;
		}
		
		private function onClose( e:Event ):void
		{
			
		}
		
		private function onClick( e:MouseEvent ):void
		{
			if( e.currentTarget==_view.btnOK )
			{
				if( _okFun != null )
				{
					_okFun();
				}
			}
			else
			{
				if( _cancelFun != null )
				{
					_cancelFun();
				}
			}
			_view.close();
		}
	}
}


















